// lib/controllers/group_detail_controller.dart
import 'package:chat/model/group_detail_model.dart';
import 'package:get/get.dart';

class GroupDetailController extends GetxController {
  final Rx<GroupDetail?> groupDetail = Rx<GroupDetail?>(null);

  @override
  void onInit() {
    super.onInit();
    loadGroupDetail();
  }

  void loadGroupDetail() {
    groupDetail.value = GroupDetail(
      id: '1',
      name: '群聊123',
      qrCodeUrl: 'https://via.placeholder.com/150',
      announcement: '这里是群公告内容',
      myNickname: '我的昵称',
      isTop: true,
      cleanMessageTime: null,
      members: [
        GroupMember(
          id: '1',
          name: 'giamdox___',
          avatarUrl: 'assets/images/zh-CN-XiaochenNeural.png',
          isAdmin: true,
        ),
        GroupMember(
          id: '2',
          name: 'giammdax...',
          avatarUrl: 'assets/images/zh-CN-XiaohanNeural.png',
          isAdmin: false,
        ),
        GroupMember(
          id: '3',
          name: 'giamddox...',
          avatarUrl: 'assets/images/zh-CN-XiaomoNeural.png',
          isAdmin: false,
        ),
        GroupMember(
          id: '4',
          name: 'giamddox...',
          avatarUrl: 'assets/images/zh-CN-XiaoqiuNeural.png',
          isAdmin: false,
        ),
        GroupMember(
          id: '5',
          name: 'giamaldo.',
          avatarUrl: 'assets/images/zh-CN-XiaoruiNeural.png',
          isAdmin: false,
        ),
        GroupMember(
          id: '6',
          name: 'giamaldo.',
          avatarUrl: 'assets/images/zh-CN-XiaoshuangNeural.png',
          isAdmin: false,
        ),
      ],
    );
  }

  void toggleTop() {
    if (groupDetail.value != null) {
      groupDetail.value = groupDetail.value!.copyWith(isTop: !groupDetail.value!.isTop);
    }
  }

  void setCleanMessageTime(String time) {
    if (groupDetail.value != null) {
      groupDetail.value = groupDetail.value!.copyWith(cleanMessageTime: time);
    }
  }

  void clearChatHistory() {
    // 实现清除聊天记录逻辑
    Get.snackbar('提示', '聊天记录已清除');
  }

  void dismissGroup() {
    // 实现解散群组逻辑
    Get.snackbar('提示', '群组已解散');
    Get.back();
  }

  void setRemark() {}
}

// 扩展方法用于更新不可变对象
extension GroupDetailExtension on GroupDetail {
  GroupDetail copyWith({
    String? id,
    String? name,
    String? qrCodeUrl,
    String? announcement,
    String? myNickname,
    bool? isTop,
    String? cleanMessageTime,
    List<GroupMember>? members,
  }) {
    return GroupDetail(
      id: id ?? this.id,
      name: name ?? this.name,
      qrCodeUrl: qrCodeUrl ?? this.qrCodeUrl,
      announcement: announcement ?? this.announcement,
      myNickname: myNickname ?? this.myNickname,
      isTop: isTop ?? this.isTop,
      cleanMessageTime: cleanMessageTime ?? this.cleanMessageTime,
      members: members ?? this.members,
    );
  }
}
